﻿Imports System.Reflection
Imports System.Collections
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms
''' <summary>
''' 操作资源文件
''' </summary>
''' <remarks></remarks>
Public Class CDllResource
    ''' <summary>
    ''' 空的构造函数
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()
        Try
            '修正默认默认路径下的问题
            _Assembly = Assembly.LoadFrom(_strDllPath)
            _Path = _strDllPath
        Catch ex As Exception
            UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod.GetType()) 'A-2009-10-11
        End Try
    End Sub

#Region "该类为单例模式 A-2009-10-11"
    Private Shared _instance As CDllResource = Nothing
    Private Shared ReadOnly padlock As New Object
    Public Shared ReadOnly Property Instance() As CDllResource
        Get
            'Lock(padlock)
            If _instance Is Nothing Then
                _instance = New CDllResource
            End If
            'Unlock(padlock)
            Return _instance
        End Get
    End Property
#End Region

    '''' <summary>
    '''' 构造函数 传递资源文件的路径
    '''' </summary>
    '''' <param name="strDllPath"></param>
    '''' <remarks></remarks>
    'Public Sub New(ByVal strDllPath As String)
    '    Try
    '        _Assembly = Assembly.LoadFrom(strDllPath)
    '        _Path = strDllPath
    '    Catch ex As Exception
    '        UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod.GetType()) 'A-2009-10-11
    '    End Try

    'End Sub
    Public Shared _strDllPath As String = ""
    Public Shared Sub SetPath(ByVal strDllPath As String)
        Try
            _strDllPath = strDllPath
        Catch ex As Exception
            UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod.GetType()) 'A-2009-10-11
        End Try

    End Sub
    '''' <summary>
    '''' 构造函数 路径 图片的大小
    '''' </summary>
    '''' <param name="strDllPath"></param>
    '''' <param name="Width"></param>
    '''' <param name="Height"></param>
    '''' <remarks></remarks>
    'Public Sub New(ByVal strDllPath As String, ByVal Width As String, ByVal Height As String)
    '    Try
    '        _Assembly = Assembly.LoadFrom(strDllPath)
    '        _Path = strDllPath
    '        Me._Width = Width
    '        Me._Height = Height
    '    Catch ex As Exception
    '        UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod.GetType()) 'A-2009-10-11
    '    End Try

    'End Sub

    '资源文件句柄
    Dim _Assembly As Assembly = Nothing
    '资源文件的路径
    Dim _Path As String = ""
    '图片的高度
    Dim _Width As String = 0
    '图片的高度
    Dim _Height As String = 0
    '图片的后缀
    Dim _ImageExt As String() = {".ico", ".gif", ".bmp", ".png", ".tif", ".jpeg", ".jpe", ".jpg"}
    '皮肤文件的后缀
    Dim _SkinExt As String() = {".ssk"}

    ''' <summary>
    ''' 判断一个资源文件是不是图片
    ''' </summary>
    ''' <param name="strFileName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function bIsImageInReasource(ByVal strFileName As String)
        bIsImageInReasource = False
        Dim strExt As String = System.IO.Path.GetExtension(strFileName).ToLower()
        For Each extName As String In _ImageExt
            If extName = strExt Then
                bIsImageInReasource = True
                Exit Function
            End If
        Next
    End Function


    ''' <summary>
    ''' 判断一个资源文件是不是皮肤
    ''' </summary>
    ''' <param name="strFileName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function bIsSkinInReasource(ByVal strFileName As String)
        bIsSkinInReasource = False
        Dim strExt As String = System.IO.Path.GetExtension(strFileName).ToLower()
        For Each extName As String In _SkinExt
            If extName = strExt Then
                bIsSkinInReasource = True
                Exit Function
            End If
        Next
    End Function

    ''' <summary>
    ''' 获取资源动态库里面的所有的图片的资源名称
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetImageNames() As String()
        Dim Names As New ArrayList
        For Each name As String In Me._Assembly.GetManifestResourceNames()
            If Me.bIsImageInReasource(name) Then
                Names.Add(name)
            End If
        Next
        GetImageNames = Names.ToArray()
    End Function

    ''' <summary>
    ''' 获取所有的图片
    ''' </summary>
    ''' <returns>返回一个图形数组</returns>
    ''' <remarks></remarks>
    Public Function GetImages() As Image()
        '
        Dim Names As New ArrayList
        For Each name As String In Me._Assembly.GetManifestResourceNames()
            If Me.bIsImageInReasource(name) Then
                Names.Add(name)
            End If
        Next

        Dim sr As System.IO.Stream
        Dim imageTemp As Image
        Dim listImage As New List(Of Image)
        For Each name As String In Names
            Try
                sr = Me._Assembly.GetManifestResourceStream(name)
                If sr IsNot Nothing Then
                    imageTemp = Image.FromStream(sr)
                    If Me._Width > 0 And Me._Height > 0 Then
                        If (imageTemp.Width = Me._Width) And (imageTemp.Height = Me._Height) Then
                            imageTemp.Tag = name
                            listImage.Add(imageTemp)
                        End If
                    Else
                        imageTemp.Tag = name
                        listImage.Add(imageTemp)
                    End If
                End If
            Catch ex As Exception
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod.GetType()) 'A-2009-10-11
            End Try
        Next
        GetImages = listImage.ToArray()
    End Function

    ''' <summary>
    ''' 返回一个图像列表
    ''' </summary>
    ''' <returns>返回一个LIST</returns>
    ''' <remarks>key 保存的是图片的在资源文件的名称 </remarks>
    Public Function GetImageList() As ImageList
        Dim imageList As New ImageList
        For Each ImageCurrent As Image In GetImages()
            imageList.Images.Add(ImageCurrent.Tag, ImageCurrent)
        Next
        GetImageList = imageList
    End Function

    ''' <summary>
    ''' 得到工具栏的图片列表
    ''' </summary>
    ''' <returns>图片列表</returns>
    ''' <remarks>key 保存的是图片的在资源文件的名称 </remarks>
    Public Function GetMenuImageList() As ImageList
        Dim imageList As New ImageList
        For Each ImageCurrent As Image In GetImages()
            If ImageCurrent.Tag.ToString().IndexOf("ImageMenuPrefix") <> -1 Then
                imageList.Images.Add(ImageCurrent.Tag, ImageCurrent)
            End If
        Next
        GetMenuImageList = imageList
    End Function

    ''' <summary>
    ''' 得到背景的图片列表
    ''' </summary>
    ''' <returns>图片列表</returns>
    ''' <remarks>key 保存的是图片的在资源文件的名称 </remarks>
    Public Function GetBackGroundImageList() As ImageList
        Dim imageList As New ImageList
        For Each ImageCurrent As Image In GetImages()
            If ImageCurrent.Tag.ToString().IndexOf("BackImagePrefix") <> -1 Then
                imageList.Images.Add(ImageCurrent.Tag, ImageCurrent)
            End If
        Next
        GetBackGroundImageList = imageList
    End Function

    ''' <summary>
    ''' 得到托盘的图片列表
    ''' </summary>
    ''' <returns>图片列表</returns>
    ''' <remarks>key 保存的是图片的在资源文件的名称 </remarks>
    Public Function GetTrayImageList() As ImageList
        Dim imageList As New ImageList
        For Each ImageCurrent As Image In GetImages()
            If ImageCurrent.Tag.ToString().IndexOf("TrayPrefix") <> -1 Then
                imageList.Images.Add(ImageCurrent.Tag, ImageCurrent)
            End If
        Next
        GetTrayImageList = imageList
    End Function

    ''' <summary>
    ''' 得到一个具体的图片
    ''' </summary>
    ''' <param name="strName">资源文件的名称</param>
    ''' <returns>返回一个图像</returns>
    ''' <remarks>异常为空</remarks>
    Public Function GetImageFormResourceName(ByVal strName As String) As Image
        GetImageFormResourceName = Nothing
        Try
            Dim sr As System.IO.Stream
            sr = Me._Assembly.GetManifestResourceStream(strName)
            GetImageFormResourceName = Image.FromStream(sr)
        Catch ex As Exception
            UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod.GetType()) 'A-2009-10-11
        End Try
    End Function

    ''' <summary>
    ''' 得到一个具体的图片
    ''' </summary>
    ''' <param name="strName">资源文件的名称</param>
    ''' <returns>返回一个图像</returns>
    ''' <remarks>异常为空</remarks>
    Public Function GetImageFormResourceStream(ByVal strName As String) As System.IO.Stream
        GetImageFormResourceStream = Nothing
        Try
            GetImageFormResourceStream = Me._Assembly.GetManifestResourceStream(strName)
        Catch ex As Exception
            UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod.GetType()) 'A-2009-10-11
        End Try
    End Function

    ''' <summary>
    ''' 返回所有皮肤文件的名称
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetSkinFileName() As String()
        Dim Names As New ArrayList
        For Each name As String In Me._Assembly.GetManifestResourceNames()
            If Me.bIsSkinInReasource(name) Then
                Names.Add(name)
            End If
        Next
        GetSkinFileName = Names.ToArray(GetType(String))
    End Function

    ''' <summary>
    ''' 得到一个具体的皮肤文件
    ''' </summary>
    ''' <param name="strName">资源文件的名称</param>
    ''' <returns>返回一个图像</returns>
    ''' <remarks>异常为空</remarks>
    Public Function GetSinFileFormResourceName(ByVal strName As String) As System.IO.Stream
        GetSinFileFormResourceName = Nothing
        Try
            Dim sr As System.IO.Stream
            sr = Me._Assembly.GetManifestResourceStream(strName)
            GetSinFileFormResourceName = sr
        Catch ex As Exception
            UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod.GetType()) 'A-2009-10-11
        End Try
    End Function

End Class
